package com.mrd.gtimp.base.action;

import com.mrd.constant.MsgConstants;
import com.mrd.exception.SaveException;
import com.mrd.framework.config.web.json.Result;
import com.mrd.framework.ui.Jqgrid;
import com.mrd.framework.ui.filter.SearchRule;
import com.mrd.framework.ui.filter.SearchRuleOption;
import com.mrd.gtimp.base.entity.Question;
import com.mrd.gtimp.base.entity.User;
import com.mrd.gtimp.base.service.QuestionBiz;

import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

import javax.annotation.Resource;

/**
 * <p> application name: gtimp <p> copyright: Copyright@2012 明睿达科技有限公司
 */

/**
 * @author qiul
 */

@Controller
@RequestMapping("/base")
public class QuestionAction {
    private static final long serialVersionUID = 1L;
    @Resource
    private QuestionBiz questionBiz;

    /**
     * public String show() { try { page = initPage(QuestionBiz.findEntityCount(filters)); Order
     * order = new Order(); order.add(sidx, sord); Questions = QuestionBiz
     * .findEntityByFilter(page.getCurrentPageStartNo(), page.getPageSize(), filters, order); }
     * catch (Exception e) { e.printStackTrace(); } return SUCCESS; }
     **/

    @RequestMapping("/ques-show")
    public String show() {
        return "/gtimp/base/ques/quesList";
    }

    @RequestMapping("/ques-quesList")
    @ResponseBody
    public Page<Question> quesList(User user, Jqgrid jqgrid) {
        try {
            if (!"0".equals(user.getType())) {
                jqgrid.addRule(new SearchRule("uid", SearchRuleOption.eq, user.getId()));
            }
//            page = initPage(questionBiz.findEntityCount(filters));
//            queses = questionBiz.findByJqgrid(page.getCurrentPageStartNo(), page
//                    .getPageSize(), filters, null);
         Page<Question> queses = questionBiz.findByJqgrid(jqgrid);
            if (queses != null && queses.getSize() > 0) {
                for (Question q : queses) {
                    q.setAnstotal(q.getAnswers().size());
                }
            }
            return queses;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/ques-save")
    @ResponseBody
    public Result save(User user,Question ques) {
        Result msg;
        try {
            ques.setId(null);
            ques.setUid(user.getId());
            ques.setIsanswer("0");
            ques.setIscheck("0");
            ques.setSendtime(new Date());
            questionBiz.saveOrupdateEntity(ques);
            msg = Result.newSuccessInstance(MsgConstants.SAVE_SUECCES,"提交成功");
        } catch (SaveException e) {
            e.printStackTrace();
            msg = Result.newFailInstance(MsgConstants.SAVE_FAIL,"提交失败");
        }
        return msg;
    }


    /**
     * set get methods
     **/

}
